package com.server.libs;

import java.math.BigDecimal;

public class PositionUtil {
    private static final double EARTH_RADIUS = 6371.0; // 地球半径，单位：千米

    public static double calculateDistance(BigDecimal lat1, BigDecimal lon1, BigDecimal lat2, BigDecimal lon2) {
        if (lat1.doubleValue() == 0 || lon1.doubleValue() == 0 || lat2.doubleValue() == 0 || lon2.doubleValue() == 0) {
            return -1.0;
        }
        double longitude1 = lon1.doubleValue() * 0.01745329251994329;
        double latitude1 = lat1.doubleValue() * 0.01745329251994329;
        double longitude2 = lon2.doubleValue() * 0.01745329251994329;
        double latitude2 = lat2.doubleValue() * 0.01745329251994329;
        double var1 = Math.sin(longitude1);
        double var2 = Math.sin(latitude1);
        double var3 = Math.cos(longitude1);
        double var4 = Math.cos(latitude1);
        double var5 = Math.sin(longitude2);
        double var6 = Math.sin(latitude2);
        double var7 = Math.cos(longitude2);
        double var8 = Math.cos(latitude2);
        double[] var10 = new double[3];
        double[] var20 = new double[3];
        var10[0] = var4 * var3;
        var10[1] = var4 * var1;
        var10[2] = var2;
        var20[0] = var8 * var7;
        var20[1] = var8 * var5;
        var20[2] = var6;

        return Math.asin(Math.sqrt((var10[0] - var20[0]) * (var10[0] - var20[0]) + (var10[1] - var20[1]) * (var10[1] - var20[1]) + (var10[2] - var20[2]) * (var10[2] - var20[2])) / 2.0) * 1.27420015798544E7;
        // 结果四舍五入 保留2位小数
        //return new BigDecimal(distance).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

}
